/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sun.courier.view;

import com.sun.courier.dal.CourierDAL;
import com.sun.courier.dal.CourierManageDAL;
import com.sun.courier.dal.DepartmentDAL;
import com.sun.courier.dto.Courier_CourierManageDTO;
import com.sun.courier.entity.Courier;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HONG QUAN
 */
public class ExportingStoreActions extends JDialog implements ActionListener{
    CourierDAL cDal = new CourierDAL();
    CourierManageDAL cmDal = new CourierManageDAL();
    DepartmentDAL dDal = new DepartmentDAL();
    
    JPanel pnMain = new JPanel();
    JPanel pnTitle = new JPanel();
    JPanel pnCenter = new JPanel();
    JPanel pnButton = new JPanel();

    JLabel lblTitle = new JLabel("Courier Returned Customer");
    JLabel lblCourierId = new JLabel("Courier Id");
    JLabel lblCourierName = new JLabel("Courier Name");
    JLabel lblCustomerName = new JLabel("Customer Name");
    JLabel lblPhone = new JLabel("Phone");
    JLabel lblWeight = new JLabel("Weight");
    JLabel lblDepartLocation = new JLabel("Departed Location");
    JLabel lblDestination = new JLabel("Destination");
    JLabel lblDistance = new JLabel("Distance");
    JLabel lblSender = new JLabel("Sender");
    JLabel lblReceiver = new JLabel("Receiver");
    
    JTextField txtCourierId = new JTextField();
    JTextField txtCourierName = new JTextField();
    JTextField txtCustomerName = new JTextField();
    JTextField txtPhone = new JTextField();
    JTextField txtWeight = new JTextField();
    JTextField txtDepartLocation = new JTextField();
    JTextField txtDestination = new JTextField();
    JTextField txtDistance = new JTextField();
    JTextField txtSender = new JTextField();
    JTextField txtReceiver = new JTextField();
    JTextField txtSenderShow = new JTextField();
    JTextField txtReceiverShow = new JTextField(); 
    
    JButton btnConfirm = new JButton("OK");

    DefaultTableModel model = null;
    int row = -1;

    public ExportingStoreActions(DefaultTableModel model, int row) {
        this.model = model;
        this.row = row;
        registerListeners();
        createControlPanel();
    }
    
    public void createControlPanel(){
        pnTitle.setLayout(new FlowLayout());
        pnTitle.setBorder(BorderFactory.createEtchedBorder());
        lblTitle.setFont(new Font("Arial", Font.BOLD, 25));
        pnTitle.add(lblTitle);

        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        pnCenter.setBorder(BorderFactory.createEtchedBorder());
        pnCenter.setLayout(gridbag);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 3, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        pnCenter.add(lblCourierId, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 0, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        txtCourierId.setEditable(false);
        txtCourierId.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtCourierId, c);

        c.gridx = 0;
        c.gridy = 2;
        pnCenter.add(lblCourierName, c);

        c.gridx = 1;
        c.gridy = 2;
        txtCourierName.setEditable(false);
        txtCourierName.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtCourierName, c);

        c.gridx = 0;
        c.gridy = 3;
        pnCenter.add(lblCustomerName, c);

        c.gridx = 1;
        c.gridy = 3;
        txtCustomerName.setEditable(false);
        txtCustomerName.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtCustomerName, c);

        c.gridx = 0;
        c.gridy = 4;
        pnCenter.add(lblPhone, c);

        c.gridx = 1;
        c.gridy = 4;
        txtPhone.setEditable(false);
        txtPhone.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtPhone, c);

        c.gridx = 0;
        c.gridy = 5;
        pnCenter.add(lblWeight, c);

        c.gridx = 1;
        c.gridy = 5;
        txtWeight.setEditable(false);
        txtWeight.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtWeight, c);

        c.gridx = 0;
        c.gridy = 6;
        pnCenter.add(lblDepartLocation, c);

        c.gridx = 1;
        c.gridy = 6;
        txtDepartLocation.setEditable(false);
        txtDepartLocation.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtDepartLocation, c);

        c.gridx = 0;
        c.gridy = 7;
        pnCenter.add(lblDestination, c);

        c.gridx = 1;
        c.gridy = 7;
        txtDestination.setEditable(false);
        txtDestination.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtDestination, c);

        c.gridx = 0;
        c.gridy = 8;
        pnCenter.add(lblDistance, c);

        c.gridx = 1;
        c.gridy = 8;
        txtDistance.setEditable(false);
        txtDistance.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtDistance, c);

        c.gridx = 0;
        c.gridy = 9;
        pnCenter.add(lblSender, c);
        c.gridx = 1;
        c.gridy = 9;
        txtSenderShow.setEditable(false);
        txtSenderShow.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtSenderShow, c);
        
        c.gridx = 0;
        c.gridy = 10;
        pnCenter.add(lblReceiver, c);
        c.gridx = 1;
        c.gridy = 10;
        txtReceiverShow.setEditable(false);
        txtReceiverShow.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtReceiverShow, c);
        
        //txtSender and txtReceiver hide
        c.gridx = 1;
        c.gridy = 11;
        txtSender.setVisible(false);
        txtSender.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtSender, c);

        c.gridx = 1;
        c.gridy = 12;
        txtReceiver.setVisible(false);
        txtReceiver.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtReceiver, c);
        
        pnButton.setLayout(new FlowLayout());
        pnButton.setBorder(BorderFactory.createEtchedBorder());
        pnButton.add(btnConfirm);

        pnMain.setLayout(new BorderLayout());
        pnMain.setBackground(Color.LIGHT_GRAY);
        pnMain.setBorder(BorderFactory.createRaisedSoftBevelBorder());
        pnMain.add(pnTitle, BorderLayout.NORTH);
        pnMain.add(pnCenter, BorderLayout.CENTER);
        pnMain.add(pnButton, BorderLayout.SOUTH);

        getContentPane().add(pnMain, BorderLayout.CENTER);
    }
    
    public void setValues(ArrayList<Courier_CourierManageDTO> lst) {

        txtCourierId.setText(Integer.toString(lst.get(0).getCm().getCourierId()));
        txtCourierName.setText(lst.get(0).getC().getCourierName());
        txtCustomerName.setText(lst.get(0).getC().getCustomerName());
        txtPhone.setText(lst.get(0).getC().getPhone());
        ArrayList<Courier> clst = new ArrayList<>();
        clst = cDal.getCourierInformationById(lst.get(0).getCm().getCourierId());
        txtWeight.setText(clst.get(0).getWeight());
        txtDepartLocation.setText(clst.get(0).getDepartLocation());
        txtDestination.setText(clst.get(0).getDestination());
        txtDistance.setText(clst.get(0).getDistance());
//        txtSender.setText(Integer.toString(lst.get(0).getCm().getSender()));
        txtReceiver.setText(Integer.toString(lst.get(0).getCm().getReceiver()));
        txtSenderShow.setText(cDal.getEmployeeName(lst.get(0).getCm().getSender()));
        txtReceiverShow.setText(cDal.getEmployeeName(lst.get(0).getCm().getReceiver()));
    }
    
    private void registerListeners() {
        btnConfirm.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(btnConfirm)){
            viewCourierExportingStore();
        }
    }

    private void viewCourierExportingStore() {
        this.dispose();
    }
}
